home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15002 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: solon.com!not-for-mail
  2. From: seebs@solutions.solon.com (Peter Seebach)
  3. Newsgroups: comp.lang.c,comp.lang.c++
  4. Subject: Re: New printf - like function, is it possible?
  5. Date: 2 Apr 1996 21:37:25 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Message-ID: <4jsrpl$s9m@solutions.solon.com>
  8. References: <4eqb3a$2r5@pan.otol.fi> <311240d2.593861@news.demon.co.uk> <3161918C.72D4@halcyon.com>
  9. NNTP-Posting-Host: solutions.solon.com
  10.  
  11. In article <3161918C.72D4@halcyon.com>,
  12. Glen Parker  <glenebob@halcyon.com> wrote:
  13. >Try this code:
  14.  
  15. >char*         Text;
  16.  
  17. >Text = (char*)malloc(6);    //get memory
  18. >sprintf(Text, "Hello");     //and stuff it
  19. >                            //with formatted text
  20.  
  21. This demonstrates why crossposts between clc and clc++ are a *bad* idea.
  22. The first code line here has a superfluous cast; all three comments are
  23. syntax errors in C.
  24.  
  25.  
  26. >MessageBox(GetFocus(), Text, "Debug", 0);
  27. >free((void*)Text);          //and free it again
  28.  
  29. Once again, superfluous cast and syntax error.
  30.  
  31. >here is the documentation I got from my Borland C++ help file:
  32.  
  33. (Perfectly reasonable description of sprintf elided.)
  34.  
  35. >On error, sprintf returns EOF.
  36.  
  37. Just as a note, printf (in ANSI) returns a negative value (not necessarily
  38. EOF) on error, and sprintf does not describe any error conditions.  This
  39. behavior is an extension.
  40.  
  41. I don't think this really answers the question; the key issue here
  42. is making a variadic function.
  43.  
  44. Try
  45.  
  46.     #include <stdarg.h>
  47.  
  48.     int
  49.     debug(char *fmt, ...) {
  50.         char buf[2048]; /* arbitrary! beware... */
  51.         va_list ap;
  52.         int ret;
  53.  
  54.         va_start(ap, fmt);
  55.         ret = vsprintf(buf, fmt, ap);
  56.         va_end(ap);
  57.  
  58.         foo(buf); /* your output-plain-string function */
  59.  
  60.         return ret;
  61.     }
  62.  
  63. The details are left as an exercise for the reader.
  64.  
  65. -s
  66. -- 
  67. Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
  68. C/Unix wizard -- C/Unix questions? Send mail for help.  No, really!
  69. FUCK the communications decency act.  Goddamned government.  [literally.]
  70. The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
  71.